

public class Tile {

	protected int x, y;
	protected final int FOOD =1, METAL = 2, POWER = 3;
	protected int metal, power, food;
	protected Tileable[] tiles;
	protected int tileables;
	protected char label, seenLabel, terrain;
	protected boolean visible, locked, structure;
	/*
	 * Seen Labels
	 * 	X = unknown
	 *  _ = Passable ground
	 * 	* = Inpassable ground
	 * 	# = just a change
	 */
	public Tile(int _x, int _y){
		this.x = _x;
		this.y = _y;
	}
	
	public Tile(int _x, int _y, char _label){
		this.x = _x;
		this.y = _y;
		this.food = 100;
		this.metal = 100;
		this.power = 100;
		this.label = _label;
		this.terrain = label;
		this.seenLabel = 'X';
		this.tiles = new Tileable[25];
		tileables = 0;
		structure = locked = false;
		
	}
	
	// ========== GETTERS ============
	public String toString(){
		if(visible)
			seenLabel=label;
		return seenLabel+"";
	}
	public boolean assigned(){return locked;}
	public int getX(){return x;}
	public int getY(){return y;}
	
	
	// ========== SETTERS ============
	public void invisible(){visible = false;}
	public void terrain(){label = terrain;}
	public void setLabel(char _label){label = _label;}
	public void unassign(){ locked = false;label = terrain;}
	
	public boolean addTileable(Tileable temp){

		int i;
		if((temp.getType()/10 == 0 && !structure) || (temp.getType()/10 != 0)){
			
			for(i = 0; tiles[i] != null ; i++);
			
			tiles[i] = temp;
			tileables++;
			return true;
		}
		return false;
	}
	
	public boolean removeTileable(Tileable temp){

		int i;
			
		for(i = 0; !tiles[i].equals(temp) && i<=25; i++);
		
		if(i != 26){
			tiles[i] = null;
			tileables--;
			if(tileables ==0)
				label = terrain;
			else{
				for(i = 0; tiles[i]==null && i<=25; i++);
				label = tiles[i].getLabel();
			}

			return true;
		}
		tileables=0;
		return false;
	}
	
	
	// ========== DO STUFF ===========
	public void visible(){
		visible = true;
		seenLabel = label;
	}	
	
	public boolean assign(){
		if(!locked){
			locked = true;
			return true;
		}
		
		return false;
	}
	
	public int harvest(int type, int x){
		int temp=0;

		if(type == METAL){
			if(metal - x >=0){
				metal -= x;
				temp = x;
			}
			else{
				temp = metal;
				metal = 0;
			}
			
			return temp;
		}
		else if (type == POWER){
			
			if(power - x >=0){
				metal -= x;
				temp = x;			
			}
			else{
				temp = power;
				power = 0;
			}
			return temp;
		}
		else if(type == FOOD){
			if(food - x >=0){
				food -= x;
				temp = x;			
			}
			else{
				temp = food;
				food = 0;
			}
			return temp;
		}
		return -1;
	}
}
